########################################################################
import re
import urllib.request
import config
import os
import time
from utils import utils
from recorder import recorder
class part2:
    """"""
    decode = ''
    readUrlList = []
    downInfoHtmlList = []
    downCurrentUrlList= []
    downPictureHtmlList = []
    
    stepNum = 0
    #----------------------------------------------------------------------
    def __init__(self,logInputDirName):
        """ get all book url from book index """
        pass
    #----------------------------------------------------------------------
    def run(self):
        """"""
        self.getPicHtml()
        self.getPicUrl()
        
    #----------------------------------------------------------------------
    def getPicHtml(self):
        """"""
        parentDir,dirNames,fileNames = os.walk(config.dirPathLogSplit);
        picHtmlFileNames = []
        print('read split dir : ',splitDir )
        for fileName in fileNames :
            if(fileName in picHtmlFileNames):
                print("bak end :",fileName)
                continue
            else:
                print("create :",fileName,publishutilitie.getLocationTime())
            asIndexUrlLog = self.logRecorder.readLogToList("%s/%s" % tuple([self.splitLogFileDir,fileName]))
            PicUrlLogFile = ("%s/%s" % tuple([self.picHtmlLogFileDir,fileName]))
            for urlInfo in asIndexUrlLog :
                url = urlInfo.split(configmap.splitLogString)
                if(url[0] != '\n'):
                    picUrl = self.getTreasureUrls(url[0])
                    self.logRecorder.wirteLog(PicUrlLogFile,picUrl)
        
        
    #----------------------------------------------------------------------
    def getTreasureUrls(self,downUrl):
        """"""
        content = utils.getHtmlFromUrl(downUrl)
        m = re.match(self.hostUrlRule,downUrl)
        downRelativeUrl = m.group(0)
        picTreeStrList = re.findall(self.pictreeListRule,content)
        picList = []
        returnUrl = []
        for picTreeStr  in picTreeStrList :
            self.logRecorder.wirteLog('picTreeStr.log',picTreeStr)
            picTreeList = re.sub("'","",picTreeStr)
            picList = picTreeList.split(",")
            for url in picList:
                returnUrl.append('%s%s' % tuple([downRelativeUrl,url]))
        return returnUrl
    
    def getPicUrl(self):
        picDir = os.path.join(self.logRecorder.LogFilePath,self.picLogFileDir)
        picFileNames = []
        for parentDir,dirNames,picFileNames in os.walk(picDir):
            print(picFileNames)
            print('read pic dir : ',picFileNames )
        picHtmlDir = os.path.join(self.logRecorder.LogFilePath,self.picHtmlLogFileDir)
        for parentDir,dirNames,picHtmlFileNames in os.walk(picHtmlDir):
            print('getPicUrl read dir : ', picHtmlDir )
            for fileName in picHtmlFileNames :
                if(fileName in picFileNames):
                    print("bak end :",fileName)
                    continue
                else:
                    print("create :",fileName,(end -start))
                    
                asIndexUrlLog = self.logRecorder.readLogToList("%s/%s" % tuple([self.picHtmlLogFileDir,fileName]))
                PicUrlLogFile = ("%s/%s" % tuple([self.picLogFileDir,fileName]))
                picUrlList = []
                for urlInfo in asIndexUrlLog :
                    url = re.match('(.*?)\\n',urlInfo)
                    if(len(url.group(0)) != 0) :
                        content = utils.getHtmlFromUrl(url.group(0))
                        picUrls = re.findall(self.imgSrcForIdRuel,content,re.VERBOSE)
                        if(len(picUrls) > 0):
                            picUrlList = picUrlList + [picUrls[0]]
                        else:
                            self.logRecorder.wirteLog('aaaaaa.log',urlInfo)
                self.logRecorder.wirteLog(PicUrlLogFile,picUrlList)

    
    
    def __del__(self):
        print('down number :' , len(self.readUrlList))
        
if(__name__=='__main__'):
    import configmap
    logInputDirName = input('Enter log dir name --> ')
    treasure(configmap.WebDecode,logInputDirName).run()

